import {createRouter, createWebHistory, RouteRecordRaw} from 'vue-router'
import {message} from 'ant-design-vue'
import cookies from 'js-cookie'
import HomeView from '../views/HomeView.vue'
import AppVersion from '../views/admin/admin-appVersion.vue'
import Examine from '../views/admin/admin-Examine.vue'
import WasteType from '../views/admin/admin-wasteType.vue'
import Waste from '../views/admin/admin-waste.vue'
import Statute from '../views/admin/admin-statute.vue'
import QualificationAudit from '../views/admin/qualificationAudit.vue'
import Menu from '../views/system/system-menu.vue'
import User from '../views/system/system-user.vue'
import Role from '../views/system/system-role.vue'
import Office from '../views/system/system-office.vue'
import WorkOrder from '../views/orders/order-workOrder.vue'
import DemandApplication from '../views/orders/order-demandApplication.vue'
import DemandBill from '../views/orders/order-demandBill.vue'
import Login from '../views/Login.vue'

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'Login',
        component: Login,
        meta: {requiredLogin:false}
    },
    {
        path: '/home',
        name: 'home',
        component: HomeView,
        children: [
            {
                path: '/admin/appVersion',
                name: 'AppVersion',
                component: AppVersion
            },
            {
                path: '/admin/examine',
                name: 'Examine',
                component: Examine
            },
            {
                path: '/admin/wasteType',
                name: 'WasteType',
                component: WasteType
            },
            {
                path: '/admin/waste',
                name: 'Waste',
                component: Waste
            },
            {
                path: '/admin/statute',
                name: 'Statute',
                component: Statute
            },
            {
                path: '/admin/qualificationAudit',
                name: 'QualificationAudit',
                component: QualificationAudit
            },
            {
                path: '/system/menu',
                name: 'Menu',
                component: Menu
            },
            {
                path: '/system/user',
                name: 'User',
                component: User
            },
            {
                path: '/system/role',
                name: 'Role',
                component: Role
            },
            {
                path: '/system/office',
                name: 'Office',
                component: Office
            },
            {
                path: '/orders/workOrder',
                name: 'WorkOrder',
                component: WorkOrder
            },
            {
                path: '/orders/demandApplication',
                name: 'DemandApplication',
                component: DemandApplication
            },
            {
                path: '/orders/demandBill',
                name: 'DemandBill',
                component: DemandBill
            },
        ]
    },
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

router.beforeEach(
    (to, from, next) => {
        if (to.meta.requiredLogin == undefined){
            console.log("jinlai")
            const user = cookies.get('user');
            if (user == undefined){
                message.error("登录认证已过期！请登录")
                next("/")
            }
            const loginUser = JSON.parse(<string>user)
            if (!loginUser || JSON.stringify(loginUser) == '{}') {
                message.error("登录认证已过期！请登录")
                next("/")
            } else {
                next()
            }
        }else {
          next()   //放行
        }
    }
)

export default router
